home *** CD-ROM | disk | FTP | other *** search
/ LG Super CD / LG Super CD.iso / bitpim / bitpim-0.62-setup.exe / {app} / bitpim.exe / serial / serialjava.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-11-06  |  7.5 KB  |  213 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.3)
  3.  
  4. import javax.comm as javax
  5. from serialutil import *
  6. VERSION = '$Revision: 1.8 $'.split()[1]
  7.  
  8. def device(portnumber):
  9.     enum = javax.comm.CommPortIdentifier.getPortIdentifiers()
  10.     ports = []
  11.     while enum.hasMoreElements():
  12.         el = enum.nextElement()
  13.         if el.getPortType() == javax.comm.CommPortIdentifier.PORT_SERIAL:
  14.             ports.append(el)
  15.             continue
  16.     return ports[portnumber].getName()
  17.  
  18.  
  19. class Serial(SerialBase):
  20.     
  21.     def open(self):
  22.         if self._port is None:
  23.             raise SerialException('Port must be configured before it can be used.')
  24.         
  25.         if type(self._port) == type(''):
  26.             portId = javax.comm.CommPortIdentifier.getPortIdentifier(self._port)
  27.         else:
  28.             portId = javax.comm.CommPortIdentifier.getPortIdentifier(device(self._port))
  29.         
  30.         try:
  31.             self.sPort = portId.open('python serial module', 10)
  32.         except Exception:
  33.             msg = None
  34.             self.sPort = None
  35.             raise SerialException('Could not open port: %s' % msg)
  36.  
  37.         self._reconfigurePort()
  38.         self._instream = self.sPort.getInputStream()
  39.         self._outstream = self.sPort.getOutputStream()
  40.         self._isOpen = True
  41.  
  42.     
  43.     def _reconfigurePort(self):
  44.         if not (self.sPort):
  45.             raise SerialException('Can only operate on a valid port handle')
  46.         
  47.         self.sPort.enableReceiveTimeout(30)
  48.         if self._bytesize == FIVEBITS:
  49.             jdatabits = javax.comm.SerialPort.DATABITS_5
  50.         elif self._bytesize == SIXBITS:
  51.             jdatabits = javax.comm.SerialPort.DATABITS_6
  52.         elif self._bytesize == SEVENBITS:
  53.             jdatabits = javax.comm.SerialPort.DATABITS_7
  54.         elif self._bytesize == EIGHTBITS:
  55.             jdatabits = javax.comm.SerialPort.DATABITS_8
  56.         else:
  57.             raise ValueError('unsupported bytesize: %r' % self._bytesize)
  58.         if self._stopbits == STOPBITS_ONE:
  59.             jstopbits = javax.comm.SerialPort.STOPBITS_1
  60.         elif stopbits == STOPBITS_ONE_HALVE:
  61.             self._jstopbits = javax.comm.SerialPort.STOPBITS_1_5
  62.         elif self._stopbits == STOPBITS_TWO:
  63.             jstopbits = javax.comm.SerialPort.STOPBITS_2
  64.         else:
  65.             raise ValueError('unsupported number of stopbits: %r' % self._stopbits)
  66.         if self._parity == PARITY_NONE:
  67.             jparity = javax.comm.SerialPort.PARITY_NONE
  68.         elif self._parity == PARITY_EVEN:
  69.             jparity = javax.comm.SerialPort.PARITY_EVEN
  70.         elif self._parity == PARITY_ODD:
  71.             jparity = javax.comm.SerialPort.PARITY_ODD
  72.         else:
  73.             raise ValueError('unsupported parity type: %r' % self._parity)
  74.         jflowin = jflowout = 0
  75.         if self._rtscts:
  76.             jflowin |= javax.comm.SerialPort.FLOWCONTROL_RTSCTS_IN
  77.             jflowout |= javax.comm.SerialPort.FLOWCONTROL_RTSCTS_OUT
  78.         
  79.         if self._xonxoff:
  80.             jflowin |= javax.comm.SerialPort.FLOWCONTROL_XONXOFF_IN
  81.             jflowout |= javax.comm.SerialPort.FLOWCONTROL_XONXOFF_OUT
  82.         
  83.         self.sPort.setSerialPortParams(baudrate, jdatabits, jstopbits, jparity)
  84.         self.sPort.setFlowControlMode(jflowin | jflowout)
  85.         if self._timeout >= 0:
  86.             self.sPort.enableReceiveTimeout(self._timeout * 1000)
  87.         else:
  88.             self.sPort.disableReceiveTimeout()
  89.  
  90.     
  91.     def close(self):
  92.         if self._isOpen:
  93.             if self.sPort:
  94.                 self._instream.close()
  95.                 self._outstream.close()
  96.                 self.sPort.close()
  97.                 self.sPort = None
  98.             
  99.             self._isOpen = False
  100.         
  101.  
  102.     
  103.     def makeDeviceName(self, port):
  104.         return device(port)
  105.  
  106.     
  107.     def inWaiting(self):
  108.         if not (self.sPort):
  109.             raise portNotOpenError
  110.         
  111.         return self._instream.available()
  112.  
  113.     
  114.     def read(self, size = 1):
  115.         if not (self.sPort):
  116.             raise portNotOpenError
  117.         
  118.         read = ''
  119.         if size > 0:
  120.             while len(read) < size:
  121.                 x = self._instream.read()
  122.                 if x == -1:
  123.                     if self.timeout >= 0:
  124.                         break
  125.                     
  126.                 self.timeout >= 0
  127.                 read = read + chr(x)
  128.         
  129.         return read
  130.  
  131.     
  132.     def write(self, data):
  133.         if not (self.sPort):
  134.             raise portNotOpenError
  135.         
  136.         self._outstream.write(data)
  137.  
  138.     
  139.     def flushInput(self):
  140.         if not (self.sPort):
  141.             raise portNotOpenError
  142.         
  143.         self._instream.skip(self._instream.available())
  144.  
  145.     
  146.     def flushOutput(self):
  147.         if not (self.sPort):
  148.             raise portNotOpenError
  149.         
  150.         self._outstream.flush()
  151.  
  152.     
  153.     def sendBreak(self):
  154.         if not (self.sPort):
  155.             raise portNotOpenError
  156.         
  157.         self.sPort.sendBreak()
  158.  
  159.     
  160.     def setRTS(self, on = 1):
  161.         if not (self.sPort):
  162.             raise portNotOpenError
  163.         
  164.         self.sPort.setRTS(on)
  165.  
  166.     
  167.     def setDTR(self, on = 1):
  168.         if not (self.sPort):
  169.             raise portNotOpenError
  170.         
  171.         self.sPort.setDTR(on)
  172.  
  173.     
  174.     def getCTS(self):
  175.         if not (self.sPort):
  176.             raise portNotOpenError
  177.         
  178.         self.sPort.isCTS()
  179.  
  180.     
  181.     def getDSR(self):
  182.         if not (self.sPort):
  183.             raise portNotOpenError
  184.         
  185.         self.sPort.isDSR()
  186.  
  187.     
  188.     def getRI(self):
  189.         if not (self.sPort):
  190.             raise portNotOpenError
  191.         
  192.         self.sPort.isRI()
  193.  
  194.     
  195.     def getCD(self):
  196.         if not (self.sPort):
  197.             raise portNotOpenError
  198.         
  199.         self.sPort.isCD()
  200.  
  201.  
  202. if __name__ == '__main__':
  203.     s = Serial(0, baudrate = 19200, bytesize = EIGHTBITS, parity = PARITY_EVEN, stopbits = STOPBITS_ONE, timeout = 3, xonxoff = 0, rtscts = 0)
  204.     s.setRTS(1)
  205.     s.setDTR(1)
  206.     s.flushInput()
  207.     s.flushOutput()
  208.     s.write('hello')
  209.     print repr(s.read(5))
  210.     print s.inWaiting()
  211.     del s
  212.  
  213.